package cn.zjh.org.service.impl;

import cn.zjh.basic.exception.BusinessException;
import cn.zjh.basic.util.BaiduAuditUtils;
import cn.zjh.org.domain.Employee;
import cn.zjh.org.domain.Shop;
import cn.zjh.org.domain.ShopAuditLog;
import cn.zjh.org.dto.ShopDto;
import cn.zjh.org.mapper.EmployeeMapper;
import cn.zjh.org.mapper.ShopAuditLogMapper;
import cn.zjh.org.mapper.ShopMapper;
import cn.zjh.org.service.IShopService;
import cn.zjh.basic.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    @Transactional
    public void settlement(Shop shop) {

        //一：校验 1.空值校验(写一个) 2.格式通过正则表达式（不写） 3.校验是否入驻过
        // 3.确认密码校验（不写） - 前端已经校验了后端还有校验，前端js代码校验可能会被跳过
        //空值校验 - a.直接使用字符串的方法 b.使用apache提供的lang3包 c.使用其他工具类
        if(StringUtils.isEmpty(shop.getName())){
            throw  new BusinessException("店铺名称不能为空");
        }
        Shop dbShop = shopMapper.loadByNameAndAddress(shop);
        if(dbShop != null){//说明根据名称和地址找到了 - 这个店铺注册过了
            throw  new BusinessException("店铺已经入驻，请直接登录!如果忘记密码，请联系管理员!!!");
        }

        Boolean b = BaiduAuditUtils.TextCensor(shop.getName() + shop.getAddress());

        if(!b){
            throw new BusinessException("店铺名称不合法，请重新输入！");
        }
        if(!BaiduAuditUtils.ImgCensor("http://123.207.27.208"+shop.getLogo())){
            throw new BusinessException("图片违规，请重新上传！");
        }

        //二：添加管理员信息 - 添加了之后就可以获取自增ID
        Employee admin = shop.getAdmin();
        employeeMapper.save(admin);

        //三：将自增id设置到t_shop的admin_id
        shop.setAdminId(admin.getId());

        //四：添加Shop
        shopMapper.save(shop);

        //五：将shop_id更新到t_employee
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }

    @Override
    public void pass(ShopAuditLog log) throws MessagingException {
        //1.修改状态：1-2
        Shop shop = shopMapper.findOne(log.getShopId());
        shop.setState(2);
        shopMapper.update(shop);

        //2.添加审核日志
        log.setState(2);
        log.setAuditId(350L);
        shopAuditLogMapper.save(log);

        //3.发送激活邮件
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
        helper.setFrom("659769497@qq.com");
        //给管理员发送邮件
        Employee employee = employeeMapper.findOne(shop.getAdminId());
        helper.setTo(employee.getEmail());
        helper.setSubject("店铺激活成功!!!");
        helper.setText("<h1>店铺激活邮件!!!</h1><h3><a href='http://localhost:8080/shop/active/"+log.getShopId()
                +"'>点击该链接激活店铺</a></h3>",true);
        javaMailSender.send(message);
    }

    @Override
    public void reject(ShopAuditLog log) throws MessagingException {
        //1.修改状态：1-4
        Shop shop = shopMapper.findOne(log.getShopId());
        shop.setState(4);
        shopMapper.update(shop);

        //2.添加审核日志
        log.setState(4);
        log.setAuditId(350L);
        shopAuditLogMapper.save(log);

        //3.发送激活邮件
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
        helper.setFrom("659769497@qq.com");
        //给管理员发送邮件
        Employee employee = employeeMapper.findOne(shop.getAdminId());
        helper.setTo(employee.getEmail());
        helper.setSubject("店铺激活邮件!!!");
        helper.setText("<h1>店铺激活失败!!!</h1><h3><a href='http://localhost:8081/#/register'>点击该链接重新入驻</a></h3>",true);
        javaMailSender.send(message);
    }
    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }
}

